package com.hao.shirojwt.util.json.impl;

import com.alibaba.fastjson.JSONObject;
import com.hao.shirojwt.util.json.JsonService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.List;

@Slf4j
public class FastJsonServiceImpl implements JsonService {

    @Override
    public String toJSONString(Object object) {
        long beginTime = System.currentTimeMillis();
        String result = JSONObject.toJSONString(object);
        long time = System.currentTimeMillis() - beginTime;
        log.debug("FastJson toJSONString 序列化耗时：{}", time);
        return result;
    }

    @Override
    public Object parseObject(String jsonString) {
        long beginTime = System.currentTimeMillis();
        Object parse = JSONObject.parse(jsonString);
        long time = System.currentTimeMillis() - beginTime;
        log.debug("FastJson parseObject 序列化耗时：{}", time);
        return parse;
    }

    @Override
    public <T> T parseObject(String jsonString, Class<T> clazz) {
        long beginTime = System.currentTimeMillis();
        T t = JSONObject.parseObject(jsonString, clazz);
        long time = System.currentTimeMillis() - beginTime;
        log.debug("FastJson parseObject对象 序列化耗时：{}", time);
        return t;
    }

    @Override
    public <T> List<T> parseArray(String jsonString, Class<T> clazz) {
        long beginTime = System.currentTimeMillis();
        List<T> ts = JSONObject.parseArray(jsonString, clazz);
        long time = System.currentTimeMillis() - beginTime;
        log.debug("FastJson parseArray 序列化耗时：{}", time);
        return ts;
    }
}
